package com.projectplace.GUI.adapter;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.File;
import java.util.Calendar;
import java.util.List;
import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.api.UserApi;
import com.projectplace.api.objectResponses.DocumentsApi.DocumentComments;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class Document_Comments_Adapter extends BaseAdapter{



		
		private List<DocumentComments> comments;
		@SuppressWarnings("unused")
		private final String LOG_TAG = "Conversation_ProjectConversation_Adapter";
		private Activity activity;
		private static LayoutInflater inflater = null;

		public Document_Comments_Adapter(Activity activity,
				List<DocumentComments> comments) {
			this.activity = activity;
			this.comments = comments;
			inflater = (LayoutInflater) this.activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			if(comments != null)
				return comments.size();
			else
				return 0;
		}

		public DocumentComments getItem(int position) {
			return comments.get(position);
		}

		public long getItemId(int position) {
			return Long.valueOf(comments.get(position).getId());
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder; 
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.conversationrow, parent,
						false);
				holder = new ViewHolder();
				holder.listitem_pic = (ImageView) convertView
						.findViewById(R.id.image);
				holder.listitem_title = (TextView) convertView
						.findViewById(R.id.title);
				holder.listitem_desc = (TextView) convertView
						.findViewById(R.id.description);
				holder.listitem_date = (TextView) convertView.findViewById(R.id.date);
				
				
				convertView.setTag(holder);
				
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			final DocumentComments comment = comments.get(position);

			if (comment != null) {
				holder.listitem_title.setText(comment.getUser_id());
				
				//Can't initiate Calendar so we get the instance
				Calendar cal = Calendar.getInstance();
				//Takes millisec and the answer from the apis is in sec so multiply by 1000
	    		cal.setTimeInMillis(1000*Long.valueOf(comment.getCreated_time()));
	    		
	    		holder.listitem_date.setText(
	    	            new StringBuilder()
	    	                    // Month is 0 based so add 1
	                    		.append(cal.get(Calendar.DAY_OF_MONTH)).append("/")
	    	                    .append(cal.get(Calendar.MONTH) + 1).append("/")
	    	                    .append(cal.get(Calendar.YEAR)).append(" ")
	    	                    .append(pad(cal.get(Calendar.HOUR_OF_DAY))).append(":")
	    	                    .append(pad(cal.get(Calendar.MINUTE))));
			
				
				holder.listitem_desc.setText(comment.getText());

					Bitmap cachedBitmapFromSDCard = BitmapFactory.decodeFile(activity.getFilesDir()
							.toString()
							+ File.separatorChar
							+ comment.getUser_id() + ".png");
					if (cachedBitmapFromSDCard != null) {
						holder.listitem_pic.setImageBitmap(cachedBitmapFromSDCard);
						
					}

					else {
						UserApi.getAvatar(SingeltonPPClient.getInstance(), comment.getUser_id(),
								holder.listitem_pic);
					}
				}
			return convertView;
			}
				

		 private static String pad(int c) {
		        if (c >= 10)
		            return String.valueOf(c);
		        else
		            return "0" + String.valueOf(c);
		}
		
		static class ViewHolder {
			TextView listitem_title;
			ImageView listitem_pic;
			TextView listitem_desc;
			TextView listitem_date;
			
		}

	}
